﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace WebNetCorePhoneErp.Model.Tool
{
    /// <summary>
    /// 读取自定义配置文件keyvalue.ini
    /// </summary>
    public static class MyConfigReader
    {
        private static readonly string configContent = null;

        static MyConfigReader()
        {
            //读取自定义配置文件 sysData.ini
            string fileConfig = AppDomain.CurrentDomain.BaseDirectory + "keyvalue.ini";
            configContent = System.IO.File.ReadAllText(fileConfig);

            //移除#开头的所有行， 去掉注释行
            Regex regular = new Regex("^\\s*#\\s*.*", RegexOptions.Multiline);
            configContent = regular.Replace(configContent, "");

            //移除空白行
            Regex regular2 = new Regex("^\\s*", RegexOptions.Multiline);
            configContent = regular2.Replace(configContent, "");
        }

        /// <summary>
        /// 读取配置文件sysData.ini节点的值
        /// </summary>
        /// <param name="configName">节点名称</param>
        /// <returns>返回节点值</returns>
        public static string GetConfigValue(string configName)
        {
            //解析配置
            Regex regular3 = new Regex($"\\s*({configName})\\s+.*");
            string nodeValue = regular3.Match(configContent).Value;

            //获取值移除空格
            Regex regex4 = new Regex(configName, RegexOptions.IgnoreCase);
            nodeValue = regex4.Replace(nodeValue, "");
            return nodeValue.Trim();
        }
    }


}
